package com.dang.alex.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBConnection {
	private static String user;
	private static String password;
	private static String url;
	static {
		try {
			ClassLoader classLoader=DBConnection.class.getClassLoader();
			InputStream is =classLoader.getResourceAsStream("config/props/db.properties");
			Properties props=new Properties();
			props.load(is);
			url=props.getProperty("url");
			user=props.getProperty("user");
			password=props.getProperty("password");
			Class.forName(props.getProperty("driver"));
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	public static Connection getConnection() throws Exception {
		return DriverManager.getConnection(url, user, password);
	}
	
	public static void close(ResultSet rs,Statement stat,Connection ct) throws Exception {
		if(rs!=null) {
			rs.close();
		}
		if(stat!=null) {
			stat.close();
		}
		if(ct!=null) {
			ct.close();
		}
	}
	public static void close(Statement stat,Connection ct) throws Exception {
		if(stat!=null) {
			stat.close();
		}
		if(ct!=null) {
			ct.close();
		}
	}
}
